/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	
	import mx.events.FlexEvent;
    
	/**
	 * 在网页上生成一个div对象
	 * 
	 * 
	 */    
      
	public class EDIHTMLViewer extends EDIHtmlPlan
	{
		private var _commitProperties:Boolean=false;
		
		private static var FUNCTION_CREATEIDIV:String = 
            "document.insertScript = function ()" +
        	"{ " + 
                "if (document.createDIV==null)" + 
                "{" +
                    "createDIV = function (frameID)" +
                	"{ " +
                		"try{"+
                        "var bodyHTMLID = document.getElementsByTagName(\"body\")[0];" +
                        "var newHTMLDiv = document.createElement('div');" +
                        "newHTMLDiv.id = frameID;" +
                        "newHTMLDiv.style.position ='absolute';" +
                        "newHTMLDiv.style.backgroundColor = 'transparent';" + 
                        "newHTMLDiv.style.border = '0px';" +
                        "newHTMLDiv.style.visibility = 'hidden';" +
                        "newHTMLDiv.style.overflow = 'hidden';" +
                        "bodyHTMLID.appendChild(newHTMLDiv);" +
                        "}catch(err){}"+
                    "}" +
                "}" +
            "}";
		
		private static var FUNCTION_SETHTML:String=
			 "document.insertScript = function ()" +
        	"{ " +
                "if (document.setDIVHTML==null)" + 
                "{" + 
                    "setDIVHTML = function (frameID,value)" +
                	"{ " +
                		"try{"+
                        "document.getElementById(frameID).innerHTML=value;"+
                        "}catch(err){}"+
                    "}" +
                "}" +
            "}";
            
        private static var FUNCTION_GETHTML:String=
        	"document.insertScript = function ()" + 
        	"{" + 
        		"if(document.getDIVHTML==null){" + 
        			"getDIVHTML=function(frameID){" + 
	        			"try{" + 
	        				"return document.getElementById(frameID).innerHTML;" + 
	        			"}catch(err){}" + 
        			"}" + 
        		"}" + 
        	"}";    
		
		private static var FUNCTION_HIDEIDIV:String = 
            "document.insertScript = function ()" +
        	"{ " +
	            "if (document.hideDIV==null)" +
	            "{" +
	                "hideDIV = function (frameID)" +
                    "{" +
                    	"try{"+
                        "document.getElementById(frameID).style.visibility='hidden';" +
                        "}catch(err){}"+
                    "}" +
                "}" +
            "}";

        private static var FUNCTION_SHOWDIV:String = 
            "document.insertScript = function ()" +
        	"{ " +
	            "if (document.showDIV==null)" +
	            "{" +
	                "showDIV = function (frameID)" +
                    "{" +
                    	"try{"+
                        "document.getElementById(frameID).style.visibility='visible';" +
                        "}catch(err){}"+
                    "}" +
                "}" + 
            "}";
		
		private static var FUNCTION_MOVEDIV:String = 
            "document.insertScript = function ()" +
        	"{ " +
	            "if (document.moveDiv==null)" +
	            "{" +
	                "moveDiv = function(frameID,x,y,w,h) " + 
	                "{" +
	                	"try{"+
	                    "var frameRef=document.getElementById(frameID);" +
	                    "frameRef.style.left=x;" + 
	                    "frameRef.style.top=y;" +
	                	"frameRef.style.width=w;" +
	                	"frameRef.style.height=h;" +
	                	"}catch(err){}"+
		            "}" +
                "}" +
            "}";
		
		private static var FUNCTION_DISABLE:String =
			"document.insertScript = function ()" +
        	"{ " +
	            "document.body.oncopy=function(){event.returnValue=false;};"+
				"document.oncontextmenu=function(){return false;};"+
            "}";
		private static var FUNCTION_NODISABLE:String =
			"document.insertScript = function ()" +
        	"{ " +
	            "document.body.oncopy=function(){};"+
				"document.oncontextmenu=function(){};"+
            "}";            
			    private static var FUNCTION_SETTXTTITLE:String=
	     	"document.insertScript = function ()" + 
	     	"{" + 
	     		"if(document.setTxtTitle==null)" + 
	     		"{" + 
	     			"setTxtTitle=function(value)" + 
	     			"{" + 
	     				"try{"+
	     				"document.getElementById('txtTitle').value=value;" + 
	     				 "}catch(err){}"+
	     			"}" + 
	     		"}" + 
	     	"}";    	  
		
		private static var FUNCTION_CHOOSEUSER:String = " function selectsignalUser(destSelect){ "
	   +" var a=document.getElementById('sessionId').value; "
      +" var str=window.showModalDialog('/console/unit.do?op=selectUsersFrame&ediSessionId='+a,null,'status:no;resizable:yes;dialogHeight:400px;dialogWidth:600px;unadorne:yes'); "
    +"  if(str!=''&&!(typeof(str) == 'undefined')){    "    
      +"   var length=str.length; "
      +"   var  len = str.lastIndexOf(';'); "
		+" var ids=str.substring(0,len); "
		+" var names=str.substring(len+1,length); "
		+" var arryIds=ids.split(','); "
	+" 	var arryNames=names.split(','); "
		+" document.getElementsByName(destSelect)[0].value=  arryNames[0]; "
     +"  }"
 +" }";
 		private static var FUNCTION_CHOOSEUNIT:String = " function openUnitTree(destSelect){ "
	+" var a=document.getElementById('sessionId').value; "
    +"  var str=window.showModalDialog('/console/unit.do?op=getUnitTree&isSelect=true&ediSessionId='+a,null,'status:no;resizable:yes;dialogHeight:300px;dialogWidth:400px;unadorne:yes'); "
    +" if(str!=''&&!(typeof(str) == 'undefined')){     "   
     +"   var length=str.length; "
     +"   var  len = str.lastIndexOf('_'); "
	+"	document.getElementsByName(destSelect)[0].value =  str.substring(len+1,length); "
     +" }"

 +"}" ;
 		private static var FUNCTION_SELECTDATE:String = "function selectDate(ctrlobj){"+
		"showx = event.screenX - event.offsetX - 4 - 210 ; "+
        "showy = event.screenY - event.offsetY + 18;"+
        "newWINwidth = 210 + 4 + 18;"+
        "retval = window.showModalDialog('/app/"+EDIApplication.appName+"/jsp/common/calendar/calendardlg.htm?returnType=two', '', 'dialogWidth:197px; dialogHeight:210px; dialogLeft:'+showx+'px; dialogTop:'+showy+'px; status:no; directories:yes;scrollbars:no;Resizable=no; '  );"+
        "if( retval != null ){"+
            "ctrlobj.value = retval;"+
        "}"+
	"}";
		/**
		 * 构造 EDIHTMLViewer 对象
		 * <EDIHTMLViewer width="100%" height="100%" id="html"></EDIHTMLViewer>
		 */		
		public function EDIHTMLViewer()
		{
			super();
			//addEventListener(FlexEvent.INITIALIZE,initHandler);
		}
		
		/**
		 * 向外界提供DIV层滚动的方法 
		 * 
		 */		
		public function scrollMoveHandler():void{
			moveDIV();
		}
		
		private function initHandler(event:FlexEvent):void{
			frameId = id;
			ExternalInterface.call(FUNCTION_CREATEIDIV);
        	ExternalInterface.call(FUNCTION_HIDEIDIV);
			ExternalInterface.call(FUNCTION_SHOWDIV);
			ExternalInterface.call(FUNCTION_MOVEDIV);
			ExternalInterface.call(FUNCTION_SETHTML);
			ExternalInterface.call(FUNCTION_GETHTML);
//			ExternalInterface.call(FUNCTION_CHOOSEUSER);
//			ExternalInterface.call(FUNCTION_CHOOSEUNIT);
//			ExternalInterface.call(FUNCTION_SELECTDATE);
			
			ExternalInterface.call("createDIV", frameId);
			registerSelf();
		}
		
		override protected function createChildren():void
        {
        	super.createChildren();
        	frameId = id;
        	
        	ExternalInterface.call(FUNCTION_CREATEIDIV);
        	ExternalInterface.call(FUNCTION_HIDEIDIV);
			ExternalInterface.call(FUNCTION_SHOWDIV);
			ExternalInterface.call(FUNCTION_MOVEDIV);
			ExternalInterface.call(FUNCTION_SETHTML);
			ExternalInterface.call(FUNCTION_GETHTML);
//			ExternalInterface.call(FUNCTION_CHOOSEUSER);
//			ExternalInterface.call(FUNCTION_CHOOSEUNIT);
//			ExternalInterface.call(FUNCTION_SELECTDATE);
			
			ExternalInterface.call("createDIV", frameId);
			registerSelf();
        }
        
         
        
        override protected function commitProperties():void
        {
        	super.commitProperties();
        	if(_commitProperties==false)
        	{
        		ExternalInterface.call(this.visible?"showDIV":"hideDIV", frameId);
        		_commitProperties=true;
        	}	
        }
        
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			moveDIV();
		}
        
        private function moveDIV(): void
        {
            var localPt:Point = new Point(0, 0);
            var globalPt:Point = this.localToGlobal(localPt);
            ExternalInterface.call("moveDiv", frameId, globalPt.x, globalPt.y, this.width, this.height);
        }
        
        /**
         * 设置或读取该层（div）的值 
         * @param text
         * 
         */        
        public function set HTML(text:String):void
        {
        	ExternalInterface.call("setDIVHTML",frameId,text);
        }
             
        public function get HTML():String
        {
        	return ExternalInterface.call("getDIVHTML",frameId);
        }
        
        
        override public function set visible(visible: Boolean): void
        {
            super.visible=visible;

            if (visible)
            {
                ExternalInterface.call("showDIV", frameId);
            }
            else 
            {
                ExternalInterface.call("hideDIV", frameId);
            }
        }
        
        /**
         * 设置该层内容是否允许选择、复制等操作 
         * @param value
         * 
         */        
        public function set disable(value:Boolean):void
        {
       		ExternalInterface.call(value?FUNCTION_DISABLE:FUNCTION_NODISABLE);
        }
	}
}